﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using RpgLibrary;
using RpgLibrary.CharacterClasses;
using XRpgLibrary.SpriteClasses;

namespace RpgEditor
{
    public partial class FormEntityDetails : FormDetailBase, IDetailForm
    {
        

        #region Field Region

        CharacterData characterData = null;
        
        #endregion

        #region Property Region

        public CharacterData CharacterData
        {
            get { return characterData; }
            set { characterData = value; }
        }

        public override IRpgDataObject DataObject
        {
            get
            {
                return (IRpgDataObject)characterData;
            }
            set
            {
                characterData = (CharacterData)value;
            }
        }

        #endregion

        #region Constructor Region

        public FormEntityDetails()
        {
            InitializeComponent();

            SetDataObjectValueFunction = SetEntityValue;
            LoadDataObjectValueFunction = LoadEntityValue;

        }
        #endregion

        #region Event Handler Region

        void LoadEntityValue()
        {
            if (characterData != null)
            {
                tbName.Text = characterData.Entity.Name;
                nudStrength.Value = characterData.Entity.Strength;
                nudDexterity.Value = characterData.Entity.Dexterity;
                nudCunning.Value = characterData.Entity.Cunning;
                nudWillpower.Value = characterData.Entity.Willpower;
                nudConstitution.Value = characterData.Entity.Constitution;
                tbHealth.Text = characterData.Entity.HealthFormula;
                tbStamina.Text = characterData.Entity.StaminaFormula;
                tbMana.Text = characterData.Entity.MagicFormula;
            }
        }

        bool SetEntityValue()
        {
            if (string.IsNullOrEmpty(tbName.Text) || string.IsNullOrEmpty(tbHealth.Text) ||
                string.IsNullOrEmpty(tbStamina.Text) || string.IsNullOrEmpty(tbMana.Text))
            {
                MessageBox.Show("Name, Health Formula, Stamina Formula and Mana Formula must have values.");
                return false;
            }

            EntityData entityData = new EntityData(
                tbName.Text,
                (int)nudStrength.Value,
                (int)nudDexterity.Value,
                (int)nudCunning.Value,
                (int)nudWillpower.Value,
                (int)nudMagic.Value,
                (int)nudConstitution.Value,
                tbHealth.Text,
                tbStamina.Text,
                tbMana.Text);

            //TODO: Make this form capable of choosing an animated sprite
            AnimatedSpriteData spriteData = new AnimatedSpriteData();

            characterData = new CharacterData(entityData, spriteData);

            return true;
        }

        #endregion
    }
}